package com.fametal.ui.activity;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ActivityGroup;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;

import com.fametal.GlobalConstant.Settings;
import com.fametal.R;
import com.fametal.ui.model.BalancedInput;
import com.fametal.ui.model.BalancedOutput;
import com.fametal.ui.utils.DialogUtils;
import com.fametal.ui.utils.Log;
import com.fametal.ui.utils.Utils;
import com.fametal.ui.utils.Utils.GetFinishedListener;
import com.fametal.ui.widget.CustomSeekBar;
import com.fametal.ui.widget.CustomSeekBar.OnClickListener;

public class BalancedControlActivity extends Activity implements GetFinishedListener, OnClickListener {

	private final static String EQUALIZER_CONTROL_URL = Settings.IP + "cgi-bin/equalizer_control.cgi?channel=all&operation=getinitvalue&volume=NULL";
	private final static String RESET_URL = Settings.IP + "cgi-bin/equalizer_control.cgi?channel=1&operation=reset&volume=0";
	private final static int MAX = 14;
	private final static int MIN = -14;
	
	private BalancedInput selectedInput;
	private BalancedOutput selectedOutput;
	
	private ActivityGroup mGroup;
	private String inputValue;
	private String outputValue;
	private View lastInputView;
	private View lastOutputView;
	
	private BalancedInput videoMeeting;
	private BalancedInput outConnectPc;
	private BalancedInput dvd;
	private BalancedInput obligateOne;
	private BalancedInput outConnectMicrophone;
	private BalancedInput phoneMeeting;
	private BalancedInput obligateTwo;
	private CustomSeekBar height_pitch;
	private CustomSeekBar low_pitch;
	
	private BalancedOutput localAmplification;
	private BalancedOutput beyondMeeting;
	private BalancedOutput beyondPhone;
	private CustomSeekBar one;
	private CustomSeekBar two;
	private CustomSeekBar three;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.balanced_control);
		mGroup = (ActivityGroup)getParent();
		Utils.setGetFinishedListener(this);
		Utils.get(mGroup, EQUALIZER_CONTROL_URL, true);
		
		height_pitch = (CustomSeekBar) findViewById(R.id.height_pitch_seekbar);
		low_pitch = (CustomSeekBar) findViewById(R.id.low_pitch_seekbar);
		height_pitch.setScope(MAX, MIN);
		low_pitch.setScope(MAX, MIN);
		height_pitch.setOnClickListener(this);
		low_pitch.setOnClickListener(this);
		
		one = (CustomSeekBar) findViewById(R.id.local_amplification_volume);
		two = (CustomSeekBar) findViewById(R.id.beyond_meeting_volume);
		three = (CustomSeekBar) findViewById(R.id.beyond_phone_volume);
		one.setScope(MAX, MIN);
		two.setScope(MAX, MIN);
		three.setScope(MAX, MIN);
		one.setOnClickListener(this);
		two.setOnClickListener(this);
		three.setOnClickListener(this);
		
		videoMeeting = new BalancedInput("video_meeting", 0, 0);
		outConnectPc = new BalancedInput("out_connect_pc", 0, 0);
		dvd = new BalancedInput("dvd", 0, 0);
		obligateOne = new BalancedInput("obligate_one", 0, 0);
		outConnectMicrophone = new BalancedInput("out_connect_microphone", 0, 0);
		phoneMeeting = new BalancedInput("phone_meeting", 0, 0);
		obligateTwo = new BalancedInput("obligate_two", 0, 0);
		localAmplification = new BalancedOutput("local_amplification", 0, 0, 0);
		beyondMeeting = new BalancedOutput("beyond_meeting", 0, 0, 0);
		beyondPhone = new BalancedOutput("beyond_phone", 0, 0, 0);
	}
	
	@Override
	public void finished(String result) {
		try {
			JSONObject jsonObject = new JSONObject(result);
			JSONObject maininput = jsonObject.getJSONObject("maininput");
			JSONObject vol1 = maininput.getJSONObject("vol1");
			videoMeeting.set(vol1.getInt("hz14k"), vol1.getInt("hz16k"));
			
			JSONObject vol2 = maininput.getJSONObject("vol2");
			outConnectPc.set(vol2.getInt("hz14k"), vol1.getInt("hz16k"));
			
			JSONObject vol3 = maininput.getJSONObject("vol3");
			dvd.set(vol3.getInt("hz14k"), vol3.getInt("hz16k"));
			
			JSONObject vol4 = maininput.getJSONObject("vol4");
			obligateOne.set(vol4.getInt("hz14k"), vol4.getInt("hz16k"));
			
			JSONObject vol5 = maininput.getJSONObject("vol5");
			outConnectMicrophone.set(vol5.getInt("hz14k"), vol5.getInt("hz16k"));
			
			JSONObject vol6 = maininput.getJSONObject("vol6");
			phoneMeeting.set(vol6.getInt("hz14k"), vol6.getInt("hz16k"));
			
			JSONObject vol7 = maininput.getJSONObject("vol7");
			obligateTwo.set(vol7.getInt("hz14k"), vol7.getInt("hz16k"));
			
			JSONObject mainoutput = jsonObject.getJSONObject("mainoutput");
			JSONObject vol1_output = mainoutput.getJSONObject("vol1");
			localAmplification.set(vol1_output.getInt("hz01h"), vol1_output.getInt("hz02h"), vol1_output.getInt("hz04h"));
			JSONObject vol2_output = mainoutput.getJSONObject("vol2");
			beyondMeeting.set(vol2_output.getInt("hz01h"), vol2_output.getInt("hz02h"), vol2_output.getInt("hz04h"));
			JSONObject vol3_output = mainoutput.getJSONObject("vol3");
			beyondPhone.set(vol3_output.getInt("hz01h"), vol3_output.getInt("hz02h"), vol3_output.getInt("hz04h"));
		} catch (JSONException e) {
			e.printStackTrace();
		} 
	}
	
	@Override
	public boolean onClick(View view, int progress, int mode) {
		String name = ""; 
		String op = "";
		String value = "";
		switch (view.getId()) {
		case R.id.height_pitch_seekbar:
			name = "sfaudio1";
			value = inputValue;
			break;
		case R.id.low_pitch_seekbar:
			name = "sfaudio2";
			value = inputValue;
			break;
		case R.id.local_amplification_volume:
			name= "sdaudio1";
			value = outputValue;
			break;
		case R.id.beyond_meeting_volume:
			name= "sdaudio2";
			value = outputValue;
			break;
		case R.id.beyond_phone_volume:
			name= "sdaudio3";
			value = outputValue;
			break;
		}
		if(mode == CustomSeekBar.INCREASE){
			op = name + "+";
		}else if(mode == CustomSeekBar.DECREASE){
			op = name + "-";
		}
		if(value == null || value.equals("")){
			DialogUtils.error(getParent(), "错误提示", "请选择按钮", null);
			return false;
		}
		
		switch (view.getId()) {
		case R.id.height_pitch_seekbar:
			if(mode == CustomSeekBar.INCREASE){
				selectedInput.heightPitch += 1;
			}else if(mode == CustomSeekBar.DECREASE){
				selectedInput.heightPitch -= 1;
			}
			break;
		case R.id.low_pitch_seekbar:
			if(mode == CustomSeekBar.INCREASE){
				selectedInput.lowPitch += 1;
			}else if(mode == CustomSeekBar.DECREASE){
				selectedInput.lowPitch -= 1;
			}
			break;
		case R.id.local_amplification_volume:
			if(mode == CustomSeekBar.INCREASE){
				selectedOutput.one += 1;
			}else if(mode == CustomSeekBar.DECREASE){
				selectedOutput.one -= 1;
			}
			break;
		case R.id.beyond_meeting_volume:
			if(mode == CustomSeekBar.INCREASE){
				selectedOutput.two += 1;
			}else if(mode == CustomSeekBar.DECREASE){
				selectedOutput.two -= 1;
			}
			break;
		case R.id.beyond_phone_volume:
			if(mode == CustomSeekBar.INCREASE){
				selectedOutput.three += 1;
			}else if(mode == CustomSeekBar.DECREASE){
				selectedOutput.three -= 1;
			}
			break;
		}
		
		String url = Settings.IP + "/cgi-bin/equalizer_control.cgi?channel=" + value +"&"+"operation=" + op +"&"+"volume=" + progress;
		Utils.get(mGroup, url, false);
		return true;
	}

	public void onClick(View view) {
		view.requestFocusFromTouch();
		switch (view.getId()) {
		case R.id.volum_control:
			LinearLayout container = (LinearLayout) mGroup.getWindow().findViewById(R.id.audio_control_container);
			mGroup.getLocalActivityManager().removeAllActivities();
			Utils.setActivityContent(mGroup, "signal", container, AudioControlActivity.class);
			break;
		case R.id.video_meeting:   //视频会议
			selectedInput(view);
			setInputProgress(videoMeeting);
			inputValue = "si1";
			break;
		case R.id.out_connect_pc:  //外接pc
			selectedInput(view);
            setInputProgress(outConnectPc);
            inputValue = "si2";
			break;
		case R.id.dvd:   //dvd
			selectedInput(view);
			setInputProgress(dvd);
			inputValue = "si3";
			break;
		case R.id.obligate_one:  //预留
			selectedInput(view);
			setInputProgress(obligateOne);
			inputValue = "si4";
			break;
		case R.id.out_connect_microphone:   //外接话筒
			selectedInput(view);
			setInputProgress(outConnectMicrophone);
			inputValue = "si5";
			break;
		case R.id.phone_metting:       //电话会议
			selectedInput(view);
			setInputProgress(phoneMeeting);
			inputValue = "si6";
			break;
		case R.id.obligate_two:         //预留
			selectedInput(view);
			setInputProgress(obligateTwo);
			inputValue = "si7";
			break;
		case R.id.thirty_percent_reset:   //30%复位
			reset(5);
			break;
		case R.id.half_reset:      //50%复位
			reset(7);
			break;
		case R.id.local_amplification:     //本地扩音
			selectedOutput(view);
			setOutputProgress(localAmplification);
			outputValue = "st1";
			break;
		case R.id.beyond_meeting:       //异地会场
			selectedOutput(view);
			setOutputProgress(beyondMeeting);
			outputValue = "st2";
			break;
		case R.id.beyond_phone:      //异地电话
			selectedOutput(view);
			setOutputProgress(beyondPhone);
			outputValue = "st3";
			break;
		}
	}
	
	public void setInputProgress(BalancedInput balanced) {
		selectedInput = balanced;
		height_pitch.setProgress(balanced.heightPitch);
		low_pitch.setProgress(balanced.lowPitch);
	}
	
	public void setOutputProgress(BalancedOutput balanced) {
		selectedOutput = balanced;
		one.setProgress(balanced.one);
		two.setProgress(balanced.two);
		three.setProgress(balanced.three);
	}
	
	public void reset(int progress){
		height_pitch.setProgress(progress);
		low_pitch.setProgress(progress);
		one.setProgress(progress);
		two.setProgress(progress);
		three.setProgress(progress);
		Utils.get(mGroup, RESET_URL, false);
	}
	
	public void selectedInput(View view) {
		view.setBackgroundResource(R.drawable.channel_select_down);
		if (lastInputView != null) lastInputView.setBackgroundResource(R.drawable.channel_select_up);
		lastInputView = view;
	}
	
	public void selectedOutput(View view) {
		view.setBackgroundResource(R.drawable.channel_select_down);
		if (lastOutputView != null) lastOutputView.setBackgroundResource(R.drawable.channel_select_up);
		lastOutputView = view;
	}
}
